﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using C7;
using System.Windows.Media.Imaging;
using M7;
using System.Linq;

namespace C7
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private delegate void TickerDelegate(string s);
        TickerDelegate tickerDelegate1;

        public MainWindow()
        {
            InitializeComponent();
            Info.window = this;
            Thread thrCon = new Thread(new ThreadStart(TcpConnection.Start));
            thrCon.IsBackground = true;
            thrCon.Start();
        }

        private void MainWindowLoaded(object sender, RoutedEventArgs e)
        {
            tickerDelegate1 = new TickerDelegate(ViewClock);
            Thread thrClock = new Thread(new ThreadStart(UpdateClock));
            thrClock.IsBackground = true;
            thrClock.Start();
            Thread thrStartCon = new Thread(new ThreadStart(() =>
            {
                AdminTools.GetModules();
            }));
            thrStartCon.IsBackground = true;
            thrStartCon.Start();
            Info.UsersPanel = spUsers;
            Info.messagePanel = grdWindows;

            Thread thrMusic = new Thread(new ThreadStart(() =>
            {
                Msg SendMsg = new Msg(Info.current.Port, Info.current.TableID);
                SendMsg.enumMethod = enumMusic.Lyrics;
                TcpConnection.SendingQueue.Enqueue(SendMsg);
            }));

            thrMusic.IsBackground = true;
            thrMusic.Start();

            Thread thrNews = new Thread(new ThreadStart(() =>
            {
                Msg SendMsg = new Msg(Info.current.Port, Info.current.TableID);
                SendMsg.enumMethod = MessageBoxButton.OKCancel;
                TcpConnection.SendingQueue.Enqueue(SendMsg);
            }));
            thrNews.IsBackground = true;
            thrNews.Start();
        }

        private void LeftRectMouseEnter(object sender, MouseEventArgs e)
        {
            if (ScrollStuff.RightRan || !ScrollStuff.LeftRan)
            {
                ScrollStuff.ScrollIntialize(spModuleTabsPanel, mtMenu, LayoutRoot, false);
                ScrollStuff.LeftRan = true;
            }
            else
            {
                ScrollStuff.Resume();
            }
        }

        private void LeftRectMouseLeave(object sender, MouseEventArgs e)
        {
            if (ScrollStuff.IsIntialized)
            {
                ScrollStuff.Pause();
            }
        }

        private void RightRectMouseEnter(object sender, MouseEventArgs e)
        {
            if (!ScrollStuff.RightRan || ScrollStuff.LeftRan)
            {
                ScrollStuff.ScrollIntialize(spModuleTabsPanel, mtMenu, LayoutRoot, true);
                ScrollStuff.RightRan = true;
            }

            else if (ScrollStuff.RightRan)
            {
                ScrollStuff.Resume();
            }
        }

        private void RightRectMouseLeave(object sender, MouseEventArgs e)
        {
            if (ScrollStuff.IsIntialized)
            {
                ScrollStuff.Pause();

            }
        }

        private void mtMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ZIndexStuff.Raise((ModuleTab)sender, spModuleTabsPanel);
        }

        

        private void ViewClock(string s)
        {
            lblClock.Content = s;
        }

        void UpdateClock()
        {
            while (true)
            {

                lblClock.Dispatcher.Invoke(tickerDelegate1,
                    new object[] { DateTime.Now.ToShortTimeString() }); //DateTime.Now.ToLongTimeString()
                Thread.Sleep(60000);
            }
        }

        private void ModuleTabMouseUp(object sender, MouseButtonEventArgs e)
        {
            if ((ModuleTab)sender == mtMenu)
            {
                if (Properties.Settings.Default.MenuModule)
                {
                    grdModules.Children.Add(Info.module = new MenuModule());
                }
                else
                {
                    MessageBox.Show("This is disabled by the administrator", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else if ((ModuleTab)sender == mtInternet)
            {
                if (Properties.Settings.Default.InternetModule)
                {
                    grdModules.Children.Add(Info.module = new InternetModule());
                }
                else
                {
                    MessageBox.Show("This is disabled by the administrator", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else if ((ModuleTab)sender == mtPersonalization)
            {
                if (Properties.Settings.Default.PersonalizationModule)
                {
                    grdModules.Children.Add(Info.module = new PersonalizationModule());
                }
                else
                {
                    MessageBox.Show("This is disabled by the administrator", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else if ((ModuleTab)sender == mtBigBook)
            {
                if (Properties.Settings.Default.MemoireModule)
                {
                    grdModules.Children.Add(Info.module = new MemoireModule());
                }
                else
                {
                    MessageBox.Show("This is disabled by the administrator", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else if ((ModuleTab)sender == mtGames)
            {
                if (Properties.Settings.Default.GamesModule)
                {
                    grdModules.Children.Add(Info.module = new GamesModule());
                }
                else
                {
                    MessageBox.Show("This is disabled by the administrator", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else if ((ModuleTab)sender == mtChat)
            {
                if (Properties.Settings.Default.ChatModule)
                {
                    grdModules.Children.Add(Info.module = new ChatModule());
                }
                else
                {
                    MessageBox.Show("This is disabled by the administrator", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else if ((ModuleTab)sender == mtCheckout)
            {
                if (Properties.Settings.Default.ChatModule)
                {
                    grdModules.Children.Add(Info.module = new CheckoutModule());
                }
                else
                {
                    MessageBox.Show("This is disabled by the administrator", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        private void imgAdd_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            grdWindows.Children.Add(Info.module = new LoginWindow());
        }

        private void mtLowerMarquee_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var lw = new LyricsWindow();
            lw.Lyrics = ((MarqueeText)sender).Text;
            grdWindows.Children.Add(lw);
        }

        private void imgCab_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var sb = (Storyboard)Resources["sbRevolvingWaitingCab"];
            sb.Begin(this);
            Msg SendMsg = new Msg(Info.current.Port, Info.current.TableID);
            SendMsg.enumMethod = MessageBoxButton.YesNo;
            TcpConnection.SendingQueue.Enqueue(SendMsg);
            grdWindows.Children.Add(new CallCabWindow());
        }

        private void imgFeedBack_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            grdWindows.Children.Add(new FeedbackWindow());
        }

        private void imgWaiter_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var sb = (Storyboard)Resources["sbRevolvingWaitingWaiter"];
            sb.Begin(this);

            Msg SendMsg = new Msg(Properties.Settings.Default.Port, Properties.Settings.Default.TableID);
            SendMsg.enumMethod = enumRequests.CallWaiter;
            TcpConnection.SendingQueue.Enqueue(SendMsg);
            
            //lama te5las yb2a sb.stop fe kolo
        }

        private void imgWCMen_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var sb = (Storyboard)Resources["sbRevolvingWaitingWCMen"];
            sb.Begin(this);

            Msg SendMsg = new Msg(Properties.Settings.Default.Port, Properties.Settings.Default.TableID);
            SendMsg.enumMethod = enumRequests.MaleWC;
            var customers = from cust in Info.current.customers
                            where cust.CustomerID == Info.current.CustomerID
                            select cust;
            if (customers.ToList().Count != 0)
            {
                SendMsg.obj = customers.ToList()[0];
                TcpConnection.SendingQueue.Enqueue(SendMsg);
            }
        }

        private void imgWCLadies_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var sb = (Storyboard)Resources["sbRevolvingWaitingWCWomen"];
            sb.Begin(this);

            Msg SendMsg = new Msg(Properties.Settings.Default.Port, Properties.Settings.Default.TableID);
            SendMsg.enumMethod = enumRequests.FemaleWC;
            TcpConnection.SendingQueue.Enqueue(SendMsg);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Thread thrStop = new Thread(new ThreadStart(() =>
                {
                    Msg SendMsg = new Msg(Properties.Settings.Default.Port, Properties.Settings.Default.TableID);
                    SendMsg.enumMethod = enumConnection.ConnectionDisconnect;
                    SendMsg.obj = Info.current.TableID;
                    TcpConnection.SendingQueue.Enqueue(SendMsg);
                    while (true)
                    {
                        if (TcpConnection.SendingQueue.Count == 0)
                        {
                            TcpConnection.Stop();
                            break;
                        }
                        else
                        {
                            Thread.Sleep(50);
                        }
                    }
                }));
            thrStop.Start();
        }

       

        private void English_Selected(object sender, RoutedEventArgs e)
        {
            ResourceDictionary dict = new ResourceDictionary();

            dict.Source = new Uri("..\\Resources\\Dictonary\\English-us.xaml", UriKind.Relative);

            this.Resources.MergedDictionaries.Add(dict);
        }

        private void Arabic_Selected(object sender, RoutedEventArgs e)
        {
            ResourceDictionary dict = new ResourceDictionary();

            dict.Source = new Uri("..\\Resources\\Dictonary\\Arabic-EG.xaml", UriKind.Relative);

            this.Resources.MergedDictionaries.Add(dict);
        }

        private void French_Selected(object sender, RoutedEventArgs e)
        {
            ResourceDictionary dict = new ResourceDictionary();

            dict.Source = new Uri("..\\Resources\\Dictonary\\French-FR.xaml", UriKind.Relative);

            this.Resources.MergedDictionaries.Add(dict);
        }

        private void China_Selected(object sender, RoutedEventArgs e)
        {
            ResourceDictionary dict = new ResourceDictionary();

            dict.Source = new Uri("..\\Resources\\Dictonary\\Chaines.xaml", UriKind.Relative);

            this.Resources.MergedDictionaries.Add(dict);
        }

        private void Spain_Selected(object sender, RoutedEventArgs e)
        {
            ResourceDictionary dict = new ResourceDictionary();

            dict.Source = new Uri("..\\Resources\\Dictonary\\Spanish.xaml", UriKind.Relative);

            this.Resources.MergedDictionaries.Add(dict);
        }

        private void Japan_Selected(object sender, RoutedEventArgs e)
        {
            ResourceDictionary dict = new ResourceDictionary();

            dict.Source = new Uri("..\\Resources\\Dictonary\\Japenese.xaml", UriKind.Relative);

            this.Resources.MergedDictionaries.Add(dict);
        }

        private void Portugal_Selected(object sender, RoutedEventArgs e)
        {
            ResourceDictionary dict = new ResourceDictionary();

            dict.Source = new Uri("..\\Resources\\Dictonary\\Portuguese.xaml", UriKind.Relative);

            this.Resources.MergedDictionaries.Add(dict);
        }

        private void Russian_Selected(object sender, RoutedEventArgs e)
        {
            ResourceDictionary dict = new ResourceDictionary();

            dict.Source = new Uri("..\\Resources\\Dictonary\\Russian.xaml", UriKind.Relative);

            this.Resources.MergedDictionaries.Add(dict);
        }

        private void German_Selected(object sender, RoutedEventArgs e)
        {
            ResourceDictionary dict = new ResourceDictionary();

            dict.Source = new Uri("..\\Resources\\Dictonary\\German.xaml", UriKind.Relative);

            this.Resources.MergedDictionaries.Add(dict);
        }
    }
}

public static class ScrollStuff
{
    #region declarations

    public static DoubleAnimation daScroll = new DoubleAnimation();
    private static AnimationClock acScroll;
    public static bool RightRan = false;
    public static bool LeftRan = false;
    public static bool IsIntialized = false;
    private static TranslateTransform translateSp = new TranslateTransform();
    public static Duration dScroll;
    //public static bool Left;

    #endregion

    private static Duration IntializeClock()
    {
        double temp = 1.5;
        if (acScroll.CurrentProgress != null)
            temp = 1.5 * (double)acScroll.CurrentProgress;

        return new Duration(TimeSpan.FromSeconds(temp));
    }

    public static void Pause()
    {
        //daScroll.Duration = IntializeClock();
        acScroll.Controller.Pause();
    }

    public static void Resume()
    {
        daScroll.Duration = IntializeClock();
        acScroll.Controller.Resume();
    }

    public static void Intialize()
    {
        daScroll.Duration = IntializeClock();
        acScroll.Controller.Begin();
        IsIntialized = true;
    }

    public static void ScrollIntialize(StackPanel sp, ModuleTab mt, Grid g, bool left)
    {
        if (left)
        {
            daScroll.To = AmountOfTranslation(sp, mt, g);
            //Left = true;
        }
        else
        {
            daScroll.To = 0;
        }
        daScroll.AccelerationRatio = 0.7;
        sp.RenderTransform = translateSp;
        acScroll = daScroll.CreateClock();
        translateSp.ApplyAnimationClock(TranslateTransform.XProperty, acScroll);
        if (IsIntialized)
        {
            Resume();
        }
        else
        {
            Intialize();
        }
    }

    private static double AmountOfTranslation(StackPanel sp, ModuleTab mt, Grid g)
    {
        double numberOfModules = sp.Children.Count; // not runtime
        double negativeMargins = mt.Margin.Right * (numberOfModules - 1); // not runtime
        double modulesShown = g.ActualWidth / mt.ActualWidth; //runtime
        return - ((numberOfModules - modulesShown) * mt.ActualWidth + negativeMargins);
    }
}
public static class ZIndexStuff
{
    public static void Raise(ModuleTab mt, StackPanel sp)
    {
        foreach (ModuleTab i in sp.Children)
            if (i == mt)
                StackPanel.SetZIndex(i, 10);
            else
                StackPanel.SetZIndex(i, 1);
    }
}
